﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

using Obsidian.Edm;
using Obsidian.Runtime;
using Oak.Core.Data;
using Oak.Base;
using Oak.Model;

namespace Oak.Core.Logic
{
    public class AppKeyLG : OakLG<OakSession>
    {

        private static bool inited = false;
        private static Hashtable htAppKeysById = Hashtable.Synchronized(new Hashtable());
        private static Hashtable htAppKeysByKey = Hashtable.Synchronized(new Hashtable());

        public AppKeyLG(OakSession session) : base(session) { }


        public List<CoActAppKeyInfo> GetAppKeyList(CoActAppKeyQry qry, out ListAttrInfo listAttr, out string msg)
        {
            if (!qry.CheckPagingAttrs(40, 100, out msg))
            {
                listAttr = null;
                return null;
            }
            AppKeyDA appKeyDA = new AppKeyDA();
            List<CoActAppKeyInfo> list;
            try
            {
                list = appKeyDA.SelectList(qry, out listAttr);
            }
            catch (Exception ex)
            {
                string code = Logger.Error(ex);
                msg = "发生系统错误:" + code;
                listAttr = null;
                return null;
            }

            msg = "";
            return list;
        }


        /// <summary>
        /// 添加AppKey信息
        /// </summary>
        /// <param name="appKey">AppKey信息</param>
        /// <param name="msg">返回信息</param>
        /// <returns></returns>
        public CoActAppKeyInfo AddAppKey(CoActAppKeyInfo appKey, out string msg)
        {

            if (!VerifyAppKey(appKey, out msg))
                return null;

            appKey.Modified.Now();
            appKey.Created.Now();

            try
            {
                appKey.Save();
            }
            catch (Exception ex)
            {
                string code = Logger.Error(ex);
                msg = "发生系统错误:" + code;
                return null;
            }

            if (!LoadAppKeysCache(out msg))
                return null;

            msg = "";
            return appKey;

        }


        /// <summary>
        /// 更新AppKey信息
        /// </summary>
        /// <param name="appKey">AppKey信息</param>
        /// <param name="msg">返回信息</param>
        /// <returns></returns>
        public CoActAppKeyInfo UpdateAppKey(CoActAppKeyInfo appKey, out string msg)
        {

            if (appKey.AppKeyId.IsNull)
            {
                msg = "AppKeyId不能为空";
                return null;
            }

            long appKeyId = appKey.AppKeyId.Value;

            CoActAppKeyInfo dbAppKey = this.GetAppKeyById(appKeyId, out msg);
            if (dbAppKey == null)
                return null;

            dbAppKey.ResetAssigned();

            if (!appKey.AppKey.IsNull)
                dbAppKey.AppKey.Set(appKey.AppKey.Value);

            if (!appKey.Description.IsNull)
                dbAppKey.Description.Set(appKey.Description.Value);

            if (!VerifyAppKey(dbAppKey, out msg))
                return null;

            dbAppKey.Modified.Now();

            try
            {
                dbAppKey.Update();
            }
            catch (Exception ex)
            {
                string code = Logger.Error(ex);
                msg = "发生系统错误:" + code;
                return null;
            }

            if (!LoadAppKeysCache(out msg))
                return null;

            msg = "";
            return dbAppKey;

        }


        public bool DeleteAppKey(long appKeyId, out string msg)
        {
            CoActAppKeyInfo appKey = this.GetAppKeyById(appKeyId, out msg);
            if (appKey == null)
                return false;
            try
            {
                appKey.Delete();
            }
            catch (Exception ex)
            {
                string code = Logger.Error(ex);
                msg = "发生系统错误:" + code;
                return false;
            }

            if (!LoadAppKeysCache(out msg))
                return false;

            msg = "";
            return true;
        }


        public CoActAppKeyInfo GetAppKey(string key, out string msg)
        {
            if (!inited)
            {
                if (!LoadAppKeysCache(out msg))
                    return null;
                inited = true;
            }

            if (!htAppKeysByKey.ContainsKey(key))
            {
                msg = "该AppKey信息不存在";
                return null;
            }

            CoActAppKeyInfo appKey = (CoActAppKeyInfo)htAppKeysByKey[key];
            msg = "";
            return appKey;
        }


        public CoActAppKeyInfo GetAppKeyById(long appKeyId, out string msg)
        {
            if (!inited)
            {
                if (!LoadAppKeysCache(out msg))
                    return null;
                inited = true;
            }

            if (!htAppKeysById.ContainsKey(appKeyId))
            {
                msg = "该AppKey信息不存在";
                return null;
            }

            CoActAppKeyInfo appKey = (CoActAppKeyInfo)htAppKeysById[appKeyId];
            msg = "";
            return appKey;
        }

        private bool LoadAppKeysCache(out string msg)
        {
            AppKeyDA appKeyDA = new AppKeyDA();
            List<CoActAppKeyInfo> list;
            try
            {
                list = appKeyDA.SelectList();
            }
            catch (Exception ex)
            {
                string code = Logger.Error(ex);
                msg = "发生系统错误:" + code;
                return false;
            }
            htAppKeysById.Clear();
            htAppKeysByKey.Clear();
            foreach (CoActAppKeyInfo appKey in list)
            {
                htAppKeysById.Add(appKey.AppKeyId.Value, appKey);
                htAppKeysByKey.Add(appKey.AppKey.Value, appKey);
            }

            msg = "";
            return true;
        }

        private bool VerifyAppKey(CoActAppKeyInfo appKey, out string msg)
        {
            if (appKey.AppKey.IsNullOrWhiteSpace)
            {
                msg = "appKey不能为空";
                return false;
            }

            msg = "";
            return true;
        }

    }
}
